#!/bin/bash
#
##############################################################################
#                                                                            #
#                         Pi-Star MOTD Generator.                            #
#                                                                            #
#     Version 2.0, Code, Design and Development by Andy Taylor (MW0MWZ).     #
#                                                                            #
#                   Make it simple to update the /etc/motd.                  #
#                                                                            #
##############################################################################
#
# Make sure we are root, if we cant stop the services, the results are a bit hit and miss.
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

# Replace the static motd with a dynamic one on systems that support it
if [ -d /etc/update-motd.d ]; then
  # Below is not indented by design, or it messes up the formatting..
  cat <<'__MOTD__' > /etc/update-motd.d/50-pistar-motd
#!/bin/bash
##############################################################################
#                         Pi-Star MOTD Generator.                            #
#     Version 2.0, Code, Design and Development by Andy Taylor (MW0MWZ).     #
##############################################################################

# Get the Pi-Star Version Info
pistarCurVersion=$(awk -F "= " '/Version/ {print $2}' /etc/pistar-release)
currentIpAddress=$(hostname -I | awk '{for(i=1;i<=NF;i++) if($i ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ && $i != "127.0.0.1") {print $i; exit}}')

cat <<EOF
        [1;31m8888888b.  d8b         .d8888b.  888[1;33m
        888   Y88b Y8P        d88P  Y88b 888[1;32m
        888    888            Y88b.      888[1;36m
        888   d88P 888         "Y888b.   888888  8888b.  888d888[1;34m
        8888888P"  888 888888     "Y88b. 888        "88b 888P"[1;35m
        888        888 888888       "888 888    .d888888 888[1;37m
        888        888        Y88b  d88P Y88b.  888  888 888[1;91m
        888        888         "Y8888P"   "Y888 "Y888888 888

[0;0mThe Pi-Star Dashboard can be found at one of the following locations:
[1;34mhttp://$(hostname)/  http://$(hostname).local/   http://${currentIpAddress}/

[0;0mPi-Star's disk is read-only by default, enable read-write with "rpi-rw".
Pi-Star built by Andy Taylor (MW0MWZ), pi-star tools all start "pistar-".

                     Welcome to Pi-Star:[1;91m v${pistarCurVersion}
[0;0m
EOF
__MOTD__

  chmod +x /etc/update-motd.d/50-pistar-motd
  if [ -f /etc/motd ]; then
    rm -rf /etc/motd
  fi



else
  # Get the Pi-Star Version Info
  pistarCurVersion=$(awk -F "= " '/Version/ {print $2}' /etc/pistar-release)
  currentIpAddress=$(hostname -I | awk '{for(i=1;i<=NF;i++) if($i ~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ && $i != "127.0.0.1") {print $i; exit}}')
  # Below is not indented by design, or it messes up the formatting..
echo -e "
        [1;31m8888888b.  d8b         .d8888b.  888[1;33m
        888   Y88b Y8P        d88P  Y88b 888[1;32m
        888    888            Y88b.      888[1;36m
        888   d88P 888         \"Y888b.   888888  8888b.  888d888[1;34m
        8888888P\"  888 888888     \"Y88b. 888        \"88b 888P\"[1;35m
        888        888 888888       \"888 888    .d888888 888[1;37m
        888        888        Y88b  d88P Y88b.  888  888 888[1;91m
        888        888         \"Y8888P\"   \"Y888 \"Y888888 888

[0;0mThe Pi-Star Dashboard can be found at one of the following locations:
[1;34mhttp://$(hostname)/  http://$(hostname).local/   http://${currentIpAddress}/

[0;0mPi-Star's disk is read-only by default, enable read-write with \"rpi-rw\".
Pi-Star built by Andy Taylor (MW0MWZ), pi-star tools all start \"pistar-\".

                     Welcome to Pi-Star:[1;91m v${pistarCurVersion}
[0;0m" > /etc/motd
fi
